﻿using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using mojoPortal.Business;
using mojoPortal.Web.UI;
using ud_Utilities;

namespace ud_ImageSlideShow
{
    public partial class ModuleSelectorSetting : UserControl, ISettingControl
    {
        /// <summary>
        /// TinLT: "before ; is ModuleID, after - is PageID".
        /// </summary>
        private string selectedValue = string.Empty;

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            if (HttpContext.Current == null) { return; }
            Load += Page_Load;
            EnsureItems();
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack) return;
            PopulateLabels();
            BindSelection();
        }


        private void PopulateLabels()
        {

        }

        private void EnsureItems()
        {
            //why is this null here, its declared
            if (ddlListModule != null) return;
            ddlListModule = new CheckBoxList();
            if (Controls.Count == 0)
            {
                Controls.Add(ddlListModule);
            }
        }

        private void GetSelectedItems()
        {
            selectedValue = string.Empty;
            foreach (ListItem item in ddlListModule.Items)
            {
                if (!item.Selected) continue;
                selectedValue += item.Value + ";";
            }
            if (selectedValue.Equals(string.Empty)) return;
            selectedValue = selectedValue.Remove(selectedValue.Length - 1);
        }

        private void BindSelection()
        {
            DBUtilities repository = new DBUtilities();
            ddlListModule.DataSource = repository.GetModuleByModuleGuid(new Guid("0CD581AE-2832-4292-82D1-CB537552243B"));
            ddlListModule.DataTextField = "ModuleTitle";
            ddlListModule.DataValueField = "ModuleID";
            ddlListModule.DataBind();
            FormatModuleTitle();
            if (selectedValue.Equals(string.Empty)) return;
            //Get Module only
            if (ddlListModule.Items.Count <= 0) return;
            if (!selectedValue.Equals(string.Empty))
            {
                char[] param = { ';' };
                string[] listModuleId = selectedValue.Contains(";") ? selectedValue.Split(param) : new[] { selectedValue };
                foreach (string moduleId in listModuleId) // Get moduleId
                {
                    ListItem item = ddlListModule.Items.FindByValue(moduleId);
                    if (item != null)
                    {
                        ddlListModule.Items.FindByValue(moduleId).Selected = true;
                    }
                }
            }
            else { ddlListModule.SelectedIndex = 0; }
        }

        private void FormatModuleTitle()
        {
            foreach (ListItem item in ddlListModule.Items)
            {
                if (item.Text.Contains("</span>"))
                {
                    item.Text = Utilities.RemoveTwoColorModuleTitleText(item.Text);
                }
                Module m = new Module(Convert.ToInt32(item.Value));
                item.Text += @" (Site " + m.SiteId + @")";
            }
        }

        #region ISettingControl

        public string GetValue()
        {
            EnsureItems();
            GetSelectedItems();
            return selectedValue;
        }

        public void SetValue(string val)
        {
            EnsureItems();
            selectedValue = val;
            BindSelection();
        }

        #endregion

    }
}